<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-promotion"></i> 评论管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户评论</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <div>
      <template>
        <el-input v-model="searchUsername" placeholder="请输入用户名......" style="width: 540px"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="queryComments">查询</el-button>
      </template>
    </div>
    <el-table :data="commentData">
      <el-table-column prop="id" label="评论ID" align="center" width="80"></el-table-column>
      <el-table-column prop="userName" label="用户名" align="center" width="200"></el-table-column>
      <el-table-column prop="commentContent" label="评论内容" align="center" width="400"></el-table-column>
      <el-table-column prop="gmtCreate" label="评论时间" align="center" width="160"></el-table-column>
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" icon="el-icon-delete" circle @click="openDeleteConfirm(scope.row)">
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          layout="prev, pager, next, jumper"
          :total="pageTotal">
      </el-pagination>
    </div>

  </div>
</template>

<script>
import axios from "axios";

export default {
  data(){
    return{
      commentData:[],
      currentPage:1,
      searchUsername:'',
      pageSize:'',
      pageTotal:'',
    }
  },
  methods:{
    loadComments(){
      let url = "http://localhost:8091/comments/get-allComments/"
      this.axios.get(url + this.currentPage).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.commentData = jsonResult.data.list;
          this.pageSize=jsonResult.data.pageSize;
          this.pageTotal=jsonResult.data.total;
          console.log(this.commentData)
        }
      })
    },
    queryComments(){
      let url="http://localhost:8091/comments/get-username-comments/"+this.searchUsername
      this.axios.get(url  ).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.commentData = jsonResult.data.list;
          this.pageSize=jsonResult.data.pageSize;
          this.pageTotal=jsonResult.data.total;
          console.log(this.commentData)
        }
      })
    },
    // 打开删除确认框
    openDeleteConfirm(comment) {
      let message = '将永久删除此评论，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(comment);
      }).catch(() => {
      });
    },
    // 执行删除
    handleDelete(comment) {
      let url = "http://localhost:8091/comments/delete-comment/" + comment.id;
      console.log('url = ' + url);

      axios.post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.loadComments();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadComments();
            }
          });
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.loadComments();
      console.log(`当前页: ${val}`);
    },
  },
  mounted() {
    this.loadComments();
  }
}
</script>

<style scoped>

</style>